﻿using Furion.FriendlyException;
using System.Collections.Generic;
using System.Linq;
using YFurion.Localization;
using YFurion.Localization.Extensions;

namespace YFurion.DataFilters
{
    internal class DataFilterOptions : IDataFilterOptions
    {
        public IReadOnlyList<DataFilterConfiguration> Filters => _filters;
        private readonly List<DataFilterConfiguration> _filters;
        private readonly ILocalizationManager _localizationManager;

        public DataFilterOptions(ILocalizationManager localizationManager)
        {
            _filters = new List<DataFilterConfiguration>();
            _localizationManager = localizationManager;
        }

        public void RegisterFilter(string filterName, bool isEnabledByDefault)
        {
            if (_filters.Any(f => f.FilterName == filterName))
            {
                throw Oops.Oh(string.Format(_localizationManager.GetCurrentString("筛选器名已存在"), filterName));
            }

            _filters.Add(new DataFilterConfiguration(filterName, isEnabledByDefault));
        }

        public void OverrideFilter(string filterName, bool isEnabledByDefault)
        {
            _filters.RemoveAll(f => f.FilterName == filterName);
            _filters.Add(new DataFilterConfiguration(filterName, isEnabledByDefault));
        }
    }
}
